import { getCurrent ,update} from "../../../api/company"

Page({
    data: {
        userId: "",
        userInfo: "",
        form: {
            linkman: "",
            logo: "",
            meta: "",
            msg: "",
            name: "",
            phone: "",
            status: "",
            type: "",
            userId: ""
        },
        IDcardF:'',
        IDcardB:'',

        meta: {
            legalPerson1: '',
            legalPerson2: '',
        },
        


        confirmFlag:false,
        modalFlag: false,
        checkRemark:'最终解释权归公司所有'
    },
    onLoad: function (options) {
        this.getCurrent()
    },
    attestation(){
        var tel = this.data.form.phone
        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
        if (!myreg.test(tel)) {
          wx.showToast({
            title: '请填写正确手机号',
            icon: 'none',
            duration: 500
            })
            return false
         }
        let params = {
            "address": this.data.form.address,
            "linkman": this.data.form.linkman,
            "logo": this.data.form.logo,
            "meta": this.data.form.meta,
            "msg": this.data.form.msg,
            "name": this.data.form.name,
            "phone": this.data.form.phone,
            "status": this.data.form.status,
            "type": this.data.form.type,
            "id": this.data.form.id,
            "meta": JSON.stringify(this.data.meta),
        }
        update(params).then(res =>{
            wx.navigateBack({
                delta: 1
            })
        })
    },
    getCurrent(){
        getCurrent().then(res =>{
            if(res.data.company.linkman == null){
                res.data.company.linkman = ''
            }
            if(res.data.company.phone == null){
                res.data.company.phone = ''
            }
            if(res.data.company.name == null){
                res.data.company.name = ''
            }
            this.data.form = res.data.company
            this.data.meta = JSON.parse(res.data.company.meta) 
            this.setData({
                form:this.data.form,
                meta:this.data.meta
            })
        })
    },
    onChooseAvatar(e){
        this.data.form.logo = e.detail.images[0]
        this.data.confirmFlag = true
        this.setData({
            form:this.data.form,
            confirmFlag:this.data.confirmFlag
        })
    },
    onChooseIDcardF(e){
        this.data.meta.legalPerson1 = e.detail.images[0]
        this.data.confirmFlag = true
        this.setData({
            meta:this.data.meta,
            confirmFlag:this.data.confirmFlag
        })
    },
    onChooseIDcardB(e){
        this.data.meta.legalPerson2 = e.detail.images[0]
        this.data.confirmFlag = true
        this.setData({
            meta:this.data.meta,
            confirmFlag:this.data.confirmFlag
        })
    },

    changeName(e){
        this.data.form.name = e.detail.value
        this.data.confirmFlag = true
        this.setData({
            form:this.data.form,
            confirmFlag:this.data.confirmFlag
        })
    },
    changeLinkman(e){
        this.data.form.linkman = e.detail.value
        this.data.confirmFlag = true
        this.setData({
            form:this.data.form,
            confirmFlag:this.data.confirmFlag
        })
    },
    changePhone(e){
        this.data.form.phone = e.detail.value
        this.data.confirmFlag = true
        this.setData({
            form:this.data.form,
            confirmFlag:this.data.confirmFlag
        })
    },
    updateType(e){
        this.data.form.type = e.detail.type
        this.data.confirmFlag = true
        this.setData({
            form:this.data.form,
            confirmFlag:this.data.confirmFlag
        })
    },
    showModal(){
        this.data.modalFlag = !this.data.modalFlag
        this.setData({
            modalFlag:this.data.modalFlag
        })
    },
    signOut(){
        wx.navigateTo({
            url: '/pages/safety/login',
        })
    }
});
